/**
 * @description: 定义班级 service
 * @version: 0.0.1
 * @author: z..
 * @Date: 2023-06-12 18:16:39
 */

import { Provide } from '@midwayjs/core';
import { RobotAlarmRecords } from './robot_alarm_records.entity';

import { InjectRepository, InjectDataSource } from '@midwayjs/sequelize';
import { Repository, Sequelize } from 'sequelize-typescript';

@Provide()
export class RobotAlarmRecordsModel {

  @InjectRepository(RobotAlarmRecords)
  RobotAlarmRecordsRepository: Repository<RobotAlarmRecords>;

  @InjectDataSource()
  SequelizeQuery: Sequelize;

  /**
    * 分页查询获取总条数
    * @param {*} params 
    * @returns 
    */
  async getTotalNum(params: any): Promise<any> {
    const { pageNum = 1, pageSize = 10, ...reset } = params;
    let str = "";
    if (Object.keys(reset).length) {
      str = "WHERE ";
      for (let key in reset) {
        switch (key) {
          case "searchKey":
            str += `( alarm_name LIKE '%${reset[key]}%' OR alarm_username LIKE '%${reset[key]}%') AND `
            break;
          case "alarmStyle":
            str += `alarm_style = '${reset[key]}' AND `
            break;
          case "alarmStatus":
            str += `alarm_status = '${reset[key]}' AND `
            break;
          case "filterDateType":
            str += `DATE_FORMAT(${reset[key]},'%Y-%m-%d') >= '${reset['filterDate'][0]}' AND DATE_FORMAT(${reset[key]},'%Y-%m-%d') <= '${reset['filterDate'][1]}' AND `
            break;
          default:
            break;
        }
      }
      str = str.substring(0, str.lastIndexOf('AND '))
    }
    const sql = `SELECT count(1) as num from robot_alarm_records ${str};`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 分页查询获取列表
   * @param {*} pageNum 
   * @param {*} pageSize
   * @param {*} searchKey
   * @returns 
   */
  async getDataByPage(params: any): Promise<any> {
    const { pageNum = 1, pageSize = 10, ...reset } = params;
    const _limit = (pageNum - 1) * pageSize;
    let str = "";
    if (Object.keys(reset).length) {
      str = "WHERE ";
      for (let key in reset) {
        switch (key) {
          case "searchKey":
            str += `( alarm_name LIKE '%${reset[key]}%' OR alarm_username LIKE '%${reset[key]}%') AND `
            break;
          case "alarmStyle":
            str += `alarm_style = '${reset[key]}' AND `
            break;
          case "alarmStatus":
            str += `alarm_status = '${reset[key]}' AND `
            break;
          case "filterDateType":
            str += `DATE_FORMAT(${reset[key]},'%Y-%m-%d') >= '${reset['filterDate'][0]}' AND DATE_FORMAT(${reset[key]},'%Y-%m-%d') <= '${reset['filterDate'][1]}' AND `
            break;
          default:
            break;
        }
      }
      str = str.substring(0, str.lastIndexOf('AND '))
    }

    const sql = `SELECT * FROM robot_alarm_records ${str} ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 新增任务通知
   * @param {*} alarmName 
   * @param {*} alarmContent 
   * @param {*} alarmType 
   * @param {*} alarmTime 
   * @param {*} alarmAdtime 
   * @param {*} alarmUserid 
   * @param {*} alarmInner 
   * @param {*} alarmStatus 
   * @param {*} brandId
   * @param {*} brandName
   * @param {*} projectId
   * @param {*} projectName
   * @param {*} alarmStyle
   * @param {*} alarmUsername 负责人名称
   * @returns 
   */
  async insertAlarm(params: any): Promise<any> {
    const sql = `INSERT INTO robot_alarm_records 
    SET alarm_name = '${params.alarmName}',
    alarm_content = '${params.alarmContent}',
    alarm_type = '${params.alarmType}',
    alarm_time = ${params.alarmTime},
    alarm_endtime = '${params.alarmEndtime}',
    alarm_status = '${params.alarmStatus}',
    alarm_userid = '${params.alarmUserid}',
    alarm_inner = '${params.alarmInner}',
    created_userid = ${params.createdUserid},
    created_username = '${params.createdUsername}',
    alarm_username = '${params.alarmUsername}',
    brand_id = ${params.brandId},
    brand_name = '${params.brandName}',
    project_id = ${params.projectId},
    project_name = '${params.projectName}',
    alarm_style = '${params.alarmStyle}',
    created_at = now(),
    updated_at = now();`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 更新任务通知
   * @param {*} id 
   * @param {*} alarmName 
   * @param {*} alarmContent 
   * @param {*} alarmType 
   * @param {*} alarmTime 
   * @param {*} alarmAdtime 
   * @param {*} alarmUserid 
   * @param {*} alarmInner
   * @param {*} brandId
   * @param {*} brandName
   * @param {*} projectId
   * @param {*} projectName
   * @param {*} alarmStyle
   * @param {*} alarmUsername 负责人名称
   * @returns 
   */
  async updateAlarmById(params: any): Promise<any> {
    const sql = `UPDATE robot_alarm_records 
    SET alarm_name = '${params.alarmName}',
        alarm_content = '${params.alarmContent}',
        alarm_type = '${params.alarmType}',
        alarm_time = ${params.alarmTime},
        alarm_endtime = '${params.alarmEndtime}',
        alarm_userid = '${params.alarmUserid}',
        alarm_inner = '${params.alarmInner}',
        brand_id = ${params.brandId},
        brand_name = '${params.brandName}',
        project_id = ${params.projectId},
        project_name = '${params.projectName}',
        alarm_style = '${params.alarmStyle}',
        alarm_username = '${params.alarmUsername}',
        updated_at = now()
    WHERE
        id = ${params.id}`;
        const [results] = await this.SequelizeQuery.query(sql);
        return results;
  }

  /**
   * 删除任务通知
   * @param {*} id 
   * @returns 
   */
  async delAlarmById(id: number): Promise<any> {
    const sql = `DELETE r,l from robot_alarm_records r left JOIN robot_alarm_logs l ON r.id = l.alarm_id WHERE r.id = ${id};`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
  * 根据项目id查询任务通知
  * @param {*} id
  * @returns 
  */
  async getAlarmById(id: number): Promise<any> {
    const sql = `select * from robot_alarm_records WHERE id = ${id}`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 修改任务通知状态
   * @param {*} id 
   * @param {*} alarmStatus
   * @returns 
   */
  async updateAlarmStatus(params: any): Promise<any> {
    const sql = `UPDATE robot_alarm_records SET alarm_status = '${params.alarmStatus}' WHERE id = ${params.id}`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 查询任务未进行中的通知消息
   * @param {*} alarmStatus
   * @returns 
   */
  async getDataByStatus(alarmStatus: string): Promise<any> {
    const sql = `SELECT * FROM robot_alarm_records where alarm_status = '${alarmStatus}' ORDER BY created_at DESC;`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

}